package com.example.demo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.login.Userparam;
import com.example.demo.datasource.multi.dao.accountinfomapper;
import com.example.demo.datasource.multi.entity.Accountinfoentity;
import com.example.demo.datasource.multi.entity.tag;
import com.example.demo.datasource.multi.entity.tags;
import com.example.demo.datasource.multi.entity.user;
import com.example.demo.entity.accountinfo;
import com.example.demo.entity.updatepwdinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.expression.Maps;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/account")
public class accountInfocontroller {

    @Autowired
    accountinfomapper accountinfomapper;
    @Autowired
    updatepwdinfo updatepwdinfo;


    @ResponseBody
    @RequestMapping("/getAccountInfo")
    public JSONObject getAccountInfo(HttpServletRequest request){
        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());
        log.info("获取用户信息:"+user.getUsername());

        String sql="select * from userinfo where realname = '"+user.getUsername()+"'";
        System.out.println(sql);

        Accountinfoentity theaccount= accountinfomapper.getaccountinfo(sql);

        List tag = new ArrayList<>();
        if(theaccount != null){
            String sql1="select * from tags where tagid = "+theaccount.getTags()+"";
            List<tags> tagsresult= accountinfomapper.gettags(sql1);
            System.out.println(tagsresult);


            for (tags ss:tagsresult
                 ) {
                Map<String,String> mm = new HashMap<>();
                mm.put("key", ss.getKey());
                mm.put("label",ss.getLable());
                System.out.println(mm);
                tag.add(mm);
            }
            theaccount.setTags(tag.toString());
            System.out.println(theaccount);

            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",0);
            result5.put("result",theaccount);
            result5.put("message","AccountInfo获取成功!");
            result5.put("type","success");
            System.out.println("AccountInfo获取成功！");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;
        }

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",-1);
        result5.put("result",theaccount);
        result5.put("message","AccountInfo获取失败,结果为空!");
        result5.put("type","ERROR");
        System.out.println("AccountInfo获取失败,结果为空！");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
        System.out.println(result8);

        return result8;

    }

    @ResponseBody
    @RequestMapping("/saveAccountInfo")
    public JSONObject saveAccountInfo(@RequestBody accountinfo params,HttpServletRequest request){

        System.out.println(params);
        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());
        log.info("获取用户信息:"+user.getUsername());

        String sql="select * from userinfo where realname = '"+user.getUsername()+"'";
        System.out.println(sql);

        Accountinfoentity theaccount= accountinfomapper.getaccountinfo(sql);
        System.out.println(theaccount);

        String sql1;
        String sql2;
        int theaccount1;

        if (theaccount != null){
            sql1 = "update userinfo set " +
                    "realname='"+params.getRealname()+"'," +
                    "birthday='"+params.getBirthday()+"'," +
                    "phone='"+params.getPhone()+"'," +
                    //"avatar='"+params.getAvatar()+"'," +
                    "email='"+params.getEmail()+"'," +
                    "sex= "+params.getSex()+"," +
                    "remarks='"+params.getRemarks()+ "' WHERE userid  = (select id from user where username = '"+user.getUsername()+"')";
            System.out.println(sql1);
            theaccount1= accountinfomapper.updateaccount(sql1);

        }else {
            String sql3="select id from user where username = '"+user.getUsername()+"'";
            int userid=accountinfomapper.getuserid(sql3);

            sql1 = "INSERT INTO userinfo (realname,birthday,phone,email,sex,remarks,userid) VALUES(" +
                    "'"+params.getRealname()+"'," +
                    "'"+params.getBirthday()+"'," +
                    "'"+params.getPhone()+"'," +
                    "'"+params.getEmail()+"'," +
                    " "+params.getSex()+"," +
                    "'"+params.getRemarks()+ "'," +
                    +userid+")";
            System.out.println(sql1);
            theaccount1= accountinfomapper.insertaccount(sql1);

        }
        sql2 = "update user set " +
                "realName='"+params.getRealname()+"'," +
                "awards.`user`.`desc`='"+params.getRemarks()+ "' WHERE username = '"+user.getUsername()+"'";
        System.out.println(sql2);



        int theaccount2= accountinfomapper.updateaccount(sql2);

        System.out.println(theaccount1+"|||||"+theaccount2);

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",null);
        result5.put("message","更新成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
        System.out.println(result8);

        return result8;
    }

    @ResponseBody
    @RequestMapping("/updatepwd")
    public JSONObject updatepwd(@RequestBody updatepwdinfo params,HttpServletRequest request){

        System.out.println(params);
        HttpSession session1 = request.getSession();
        //统一拦截（查询当前session是否存在user）(这里user会在每次登录成功后，写入session)
        Userparam user = (Userparam) session1.getAttribute(session1.getId());
        log.info("获取用户信息:"+user.getUsername());
        log.info("获取用户密码:"+user.getUserpassword());

        if (!params.getField1().equals(user.getUserpassword())){
            Map<String, Object> result5 = new HashMap<String,Object>();
            result5.put("code",-1);
            result5.put("result",null);
            result5.put("message","原密码错误!");
            result5.put("type","error");

            JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
            System.out.println(result8);

            return result8;
        }

        String sql1 = "update user set password = '"+params.getField2()+"' where username = '"+user.getUsername()+"'";
        System.out.println(sql1);


        int theaccount1= accountinfomapper.updateaccount(sql1);


        System.out.println(theaccount1+"|||||");

        Map<String, Object> result5 = new HashMap<String,Object>();
        result5.put("code",0);
        result5.put("result",null);
        result5.put("message","更新成功!");
        result5.put("type","success");

        JSONObject result8 = JSONObject.parseObject(JSON.toJSONString(result5));
        System.out.println(result8);

        return result8;
    }

}
